<!-- 首页 -->
<template>
	<view class="page" v-if="chat_flag == 1">
		<view class="user-box" v-for="(item, index) in dataList" :key="index">
			<u-avatar @click="chat(item)" size="large" :src="item.avatar"></u-avatar>
			<view class="user-box-right">
				<view>
					<view class="user-name-box">
						<view class="nickname">{{item.nickName}}</view>
						<view class="sex-box-nv" v-if="item.sex == 1">
							<u-icon name="ziyuan2" custom-prefix="iconfont"></u-icon>
							<view class="age-text">{{item.age}}</view>
						</view>
						<view class="sex-box" v-else>
							<u-icon name="ziyuan3" custom-prefix="iconfont"></u-icon>
							<view class="age-text">{{item.age}}</view>
						</view>
						<view class="vip-box" v-if="item.isPay == '1'">
							<u-icon name="xianxinghuiyuan" custom-prefix="iconfont"></u-icon>
							<view class="vip-text">贵族</view>
						</view>
					</view>
					<view class="user-tag-box">
						<view class="user-tag">{{item.city}}</view>
						<view class="line"></view>
						<view class="user-tag">{{item.distance}}km</view>
					</view>
					<view v-if="item.signName == '' || item.signName == null ">{{item.signName}}</view>
					<view class="sign-name" v-else>{{item.signName}}</view>
				</view>
				<view>
					<u-button type="success" size="mini" @click="addWechat(item)" v-if="item.wechatFlag">加微信</u-button>
					<u-button type="primary" size="mini" @click="apply(item)" v-else>打招呼</u-button>
				</view>
			</view>
		</view>
		
		<view class="empty-text" v-if="dataList.length == 0">暂无好友</view>
		<view class="loadmore-box" v-if="dataList.length > 0">
			<u-loadmore :status="status" @loadmore="load"></u-loadmore>
		</view>
	</view>
</template>

<script>
export default {
	props: {
		userId: {
			type: String,
			required: false,
			default: function() {
				return 0;
			}
		},
	},
	data() {
		return {
			form: {
				lon: '',
				lat: '',
			},
			status: 'loadmore',
			scrollTop: 0,
			isEnd: false,
			current: 1,
			pageSize: 10,
			dataList: [],
		};
	},
	mounted() {
		this.query();
	},
	methods: {
		reload() {
			this.query();
		},
		load() {
			this.loadmore();
		},
		async loadmore() {
			this.status = 'loading';
			if(this.isEnd){
				this.status = 'nomore';
				return;
			}
			var res = await this.getDataList();
			this.dataList = this.dataList.concat(res.rows);
		},
		query() {
			this.current = 1;
			this.isEnd = false;
			this.status = 'loadmore';
			this.loadquery();
		},
		async loadquery() {
			var res = await this.getDataList();
			this.dataList = res.rows;
		},
		async getDataList() {
			var res = await this.$u.get('/wq/userFriend/getPageList', {
				pageNum: this.current,
				pageSize: this.pageSize,
				userId: this.userId,
			});
			var totalPage = Math.ceil(res.total/this.pageSize);
			this.pages = totalPage;
			this.current ++;
			this.status = 'loadmore';
			if(this.current > this.pages){
				this.status = 'nomore';
				this.isEnd = true;
			}
			return res;
		},
		apply(e) {
			this.$u.post('/wq/friendApply/add',{
				userId: e.userId,
			}).then(res => {
				var _this = this;
				_this.getAmount();
				uni.showModal({
				    title: '温馨提示',
				    content: '需要接收好友通知吗？',
					confirmText:"同意",
					cancelText:"拒绝",
				    success: function (e) {
				        if (e.confirm) {
							_this.subscribeMessage(res.data.tmplIds);
				        } else if (e.cancel) {
							
				        }
				    }
				});
			});
		},
		getAmount() {
			this.$u.get('/pay/userCoin/getAmount').then(res => {
				this.$u.vuex('vuex_amount', res.data);
			});
		},
		addApply(e) {
			this.$u.post('/wq/friendApply/addWechat',{
				userId: e.userId,
			}).then(res => {
				this.getAmount();
				this.$u.route({
					url: 'packgeMine/pages/index/chat',
					params: {
						applyId: res.data.id,
					}
				});
			});
		},
		addWechat(item) {
			this.addApply(item);
		},
		subscribeMessage(tmplIds) {
			uni.requestSubscribeMessage({
				//数组，最多放三个订阅模板的ID
				tmplIds: tmplIds,
				success: function(res) {
					console.log(JSON.stringify(res));
				},
				fail: function() {
		
				},
				complete: (res) => {
					console.log(333,res)
				}
			});
		},
		chat(item) {
			this.judgeLogin(() => {
				this.$u.route({
					url: 'packgeMine/pages/center/index',
					params: {
						userId: item.userId,
					}
				});
			});
		},
	}
};
</script>
<style lang="scss" scoped>
	.page {
		padding-bottom: 176rpx;
		background-color: #fff;
		margin-top: 10px;
	}
	.user-box {
		display: flex;
		align-items: center;
		padding: 15px;
		padding-bottom: 0;
	}
	.user-name-box {
		display: flex;
		align-items: center;
	}
	.nickname {
		font-size: 28rpx;
		margin-right: 5px;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
		max-width: 100px;
	}
	.sex-box {
		background-color: #2b85e4;
		display: flex;
		color: #fff;
		padding: 4rpx 10rpx;
		border-radius: 20px;
		font-size: 16rpx;
	}
	.sex-box-nv {
		background-color: #fe709f;
		display: flex;
		color: #fff;
		padding: 4rpx 10rpx;
		border-radius: 20px;
		font-size: 16rpx;
	}
	.user-tag-box {
		display: flex;
		align-items: center;
	}
	.user-tag {
		font-size: 20rpx;
		color: #82848a;
		margin: 10rpx 0;
	}
	.user-box-right {
		margin-left: 15px;
		display: flex;
		align-items: center;
		justify-content: space-between;
		flex: 1;
	}
	.sign-name {
		font-size: 24rpx;
		color: #8d8d8d;
		text-overflow: ellipsis;
		width: 400rpx;
		overflow: hidden;
		white-space: nowrap;
	}
	.renzheng-box {
		display: flex;
		align-items: center;
		margin-right: 5px;
	}
	.line {
		float:left;
		width: 1px;
		height: 16rpx; 
		background: #e5e5e5;
		margin: 0 5px;
	}
	.vip-box {
		color: #fff;
		margin-left: 5px;
		font-size: 20rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: #f7ce05;
		padding: 4rpx 8rpx;
		border-radius: 20px;
	}
	.vip-text {
		font-size: 16rpx;
	}
	.empty-text {
		display: flex;
		align-items: center;
		justify-content: center;
		height: 100px;
		color: #b4adad;
	}
	.loadmore-box {
		padding: 15px;
	}
</style>
